source("recodes.R")
library(plyr)
library(ggplot2)
library(sjmisc)

# Figure 1(A)

white.coefs <- rbind(std_beta(lm(prop.white ~ ch2white + pctwhite.zip + pctwhite.cty, study), include.ci = TRUE),  std_beta(lm(prop.white ~ ch2white + pctwhite.zip + pctwhite.cty, replication), include.ci = TRUE))
black.coefs <- rbind(std_beta(lm(prop.black ~ ch2black + pctblack.zip + pctblack.cty, study), include.ci = TRUE),  std_beta(lm(prop.black ~ ch2black + pctblack.zip + pctblack.cty, replication), include.ci = TRUE))
asian.coefs <- rbind(std_beta(lm(prop.asian ~ ch2asian + pctasian.zip + pctasian.cty, study), include.ci = TRUE),  std_beta(lm(prop.asian ~ ch2asian + pctasian.zip + pctasian.cty, replication), include.ci = TRUE))
hisp.coefs <- rbind(std_beta(lm(prop.hisp ~ ch2hisp + pcthisp.zip + pcthisp.cty, study), include.ci = TRUE),  std_beta(lm(prop.hisp ~ ch2hisp + pcthisp.zip + pcthisp.cty, replication), include.ci = TRUE))

college.coefs <- std_beta(lm(prop.college ~ ch2college + pctcollege.zip + pctcollege.cty, replication), include.ci = TRUE)
senior.coefs <- std_beta(lm(prop.seniors ~ ch2seniors + pctseniors.zip + pctseniors.cty, replication), include.ci = TRUE)

coefs <- rbind(black.coefs, hisp.coefs, asian.coefs, white.coefs, college.coefs, senior.coefs)

names <- factor(c(rep(c("Black", "Hispanic", "Asian", "White"), each = 6), rep("College-Educated", 3), rep("Senior Citizens", 3)), c("Black", "Hispanic", "Asian", "White", "College-Educated", "Senior Citizens"))

measures <- factor(rep(c("User-Defined Boundary", "ZCTA", "County"), length.out = 30), c("User-Defined Boundary", "County", "ZCTA"))

stn <- c(rep(c("July 2015", "July 2016"), each = 3, length.out = 24), rep("July 2016", 6))

ses <- (coefs[,1] - coefs[,2])/1.96

df <- data.frame(coefs, ses, names, measures, stn)

limits <- aes(ymax = ci.low, ymin= ci.hi)

p <- ggplot(df, aes(y=beta, x=measures, group=names, colour=names)) + geom_pointrange(limits, position=position_dodge(width=.5)) + theme_bw() + geom_hline(yintercept = 0) + labs(x = "Measures", y = "Estimated Regression Coefficients", color = "Demographics") + scale_color_grey(start = .8, end = .4)

p + facet_grid(stn ~ .) + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())

# Figure 1(B)

white.coefs <- rbind(std_beta(lm(prop.white ~ pointswhite + combinedwhite + attachwhite + chwhite + pctwhite.zip + pctwhite.cty, study), include.ci = TRUE),  std_beta(lm(prop.white ~ naivepopwhite + combinedpopwhite + attachpopwhite + chpopwhite + pctwhite.zip + pctwhite.cty, replication), include.ci = TRUE))
black.coefs <- rbind(std_beta(lm(prop.black ~ pointsblack + combinedblack + attachblack + chblack + pctblack.zip + pctblack.cty, study), include.ci = TRUE),  std_beta(lm(prop.black ~ naivepopblack + combinedpopblack + attachpopblack + chpopblack + pctblack.zip + pctblack.cty, replication), include.ci = TRUE))
hisp.coefs <- rbind(std_beta(lm(prop.hisp ~ pointshisp + combinedhisp + attachhisp + chhisp + pcthisp.zip + pcthisp.cty, study), include.ci = TRUE),  std_beta(lm(prop.hisp ~ naivepophisp + combinedpophisp + attachpophisp + chpophisp + pcthisp.zip + pcthisp.cty, replication), include.ci = TRUE))
asian.coefs <- rbind(std_beta(lm(prop.asian ~ pointsasian + combinedasian + attachasian + chasian + pctasian.zip + pctasian.cty, study), include.ci = TRUE),  std_beta(lm(prop.asian ~ naivepopasian + combinedpopasian + attachpopasian + chpopasian + pctasian.zip + pctasian.cty, replication), include.ci = TRUE))

replication <- replication[-which(replication$naivepctcollege == Inf),]
college.coefs <- std_beta(lm(prop.college ~ naivepctcollege + combinedpctcollege + attachpctcollege + chpctcollege + pctcollege.zip + pctcollege.cty, replication, subset = naivepctcollege != Inf), include.ci = TRUE)

senior.coefs <- std_beta(lm(prop.seniors ~ naivex65plus + combinedx65plus + attachx65plus + chx65plus + pctseniors.zip + pctseniors.cty, replication), include.ci = TRUE)

coefs <- rbind(black.coefs, hisp.coefs, asian.coefs, white.coefs, college.coefs, senior.coefs)

names <- factor(c(rep(c("Black", "Hispanic", "Asian", "White"), each = 12), rep("College-Educated", 6), rep("Senior Citizens", 6)), c("Black", "Hispanic", "Asian", "White", "College-Educated", "Senior Citizens"))

measures <- factor(rep(c("Naive", "Time-Weighted", "Attachment-Weighted", "Convex Hull", "ZCTA", "County"), length.out = 60), c("Naive", "Time-Weighted", "Attachment-Weighted", "Convex Hull", "County", "ZCTA"))

stn <- c(rep(c("July 2015", "July 2016"), each = 6, length.out = 48), rep("July 2016", 12))

ses <- (coefs[,1] - coefs[,2])/1.96

df <- data.frame(coefs, ses, names, measures, stn)

limits <- aes(ymax = ci.low, ymin= ci.hi)

p <- ggplot(df, aes(y=beta, x=measures, group=names, colour=names)) + geom_pointrange(limits, position=position_dodge(width=.5)) + theme_bw() + geom_hline(yintercept = 0) + labs(x = "Measures", y = "Estimated Regression Coefficients", color = "Demographics") + scale_color_grey(start = .8, end = .4)

p + facet_grid(stn ~ .) + theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())
